package com.wyb.hzmt.static_baseFrame_tools0.framework;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.lidroid.xutils.ViewUtils;
import com.wyb.hzmt.static_baseFrame_tools0.eventbus.TestEvent;
import com.wyb.hzmt.static_baseFrame_tools0.tools.BaseLog;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;


/***
 * 方法数共13
 * 1、生命周期方法组，共2个
 * 2、onCreateView内方法组，方法数共2
 * 3、点击事件方法组，方法数共2
 * 4、eventbus的方法组，方法数共１
 */
public abstract class MyBaseFragment extends Fragment implements View.OnClickListener  {
    protected Activity baseFragmentActivity;
    private View view;

    /***
     * 1、生命周期方法组，共2个
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach ( activity );
        this.baseFragmentActivity = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initOnCreate();
    }

    protected abstract void initOnCreate();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = onLayoutView ( inflater, container, savedInstanceState );
        ViewUtils.inject(this,view);
        if (view != null)
            initView ( view, savedInstanceState );
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 2、onCreateView内方法组，方法数共2
     * 初始化布局
     */
    protected abstract View onLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
    /**
     * 页面初始化
     */
    protected abstract void initView(View container, Bundle savedInstanceState);

    /**
     * 3、点击事件方法组，方法数共2
     * 初始化布局
     */
    @Override
    public void onClick(View view) {
        onClickEvent(view);
    }
    protected abstract  void onClickEvent(View view);

    /**
     *
     * 4、eventbus的方法组，方法数共１
     * 仅仅是为了给Eventbus占位置，不然会报 你没有Subscribe方法还注册我干嘛的异常。
     * 这个异常是为了节省eventbus的开销，也挺好……
     */
    @Subscribe
    public void onEvent(TestEvent testEvent){
        BaseLog.e("TAG","Base onEvent执行了");
    }
}
